var net = require('net');

function getConnection (connName) {
	var client = net.connect({port:10000,host:'localhost'},function () {
		console.log(connName + ' Connection:');
		console.log('local = %s:%s', this.localAddress, this.localPort);
		console.log('remote = %s:%s', this.remoteAddress, this.remotePort);
		this.setTimeout(500);
		this.setEncoding('utf8');

		this.on('data', function(data) {
			console.log(connName + 'From Server:' + data.toString());
			this.end();
		});

		this.on('error', function(error) {
			console.log('Socket Error :',JSON.stringify(error));
		});

		this.on('timeout', function() {
			console.log('timeout!');
		});

		this.on('close', function() {
			console.log('closed!');
		});
	});
	return client;
}

function writeData (socket, data) {
	var success = !socket.write(data);
	if (!success) {
		(function (socket,data) {
			socket.once('drain', function () {
				writeData(socket, data);
			});
		})(socket, data);
	}
}

var Dwarver = getConnection('Dwarver');
var Elves = getConnection('Elves');
var Jpbbots = getConnection('Jpbbots');
writeData(Dwarver, 'More Axes');
writeData(Elves, 'More Arrows');
writeData(Jpbbots, 'More Pipe Weed');
